Show:
Defined in: addon/index.js:141

Default Options

Default options can be specified over a set of validations for a given attribute. Local properties will always take precedence.

Instead of doing the following:

const Validations = buildValidations({
  username: [
    validator('presence', {
      presence: true,
      description: 'Username'
    }),
    validator('length', {
      min: 1,
      description: 'Username'
    }),
    validator('no-whitespace-around', {
      description: 'A username'
    })
  ]
});

We can declare default options:

const Validations = buildValidations({
  username: {
    description: 'Username'
    validators: [
      validator('presence', true),
      validator('length', {
        min: 1
      }),
      validator('no-whitespace-around', {
        description: 'A username'
      })
    ]
  },
});

In the above example, all the validators for username will have a description of Username except that of the no-whitespace-around validator which will be A username.

Global Options

If you have specific options you want to propagate throught all your validation rules, you can do so by passing in a global options object. This is ideal for when you have a dependent key that each validator requires such as the current locale from your i18n implementation, or you want easily toggle your validations on/off.

const Validations = buildValidations(validationRules, globalOptions);
import Ember from 'ember';
import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
  firstName: {
    description: 'First Name'
    validators: [
      validator('presence', {
        presence: true,
        dependentKeys: ['foo', 'bar']
      })
     ]
   },
  lastName: validator('presence', true)
}, {
  description: 'This field'
  dependentKeys: ['i18n.locale', 'disableValidations'],
  disabled(model, attribute) {
    return model.get('disableValidations');
  }
});

Just like in the default options, locale validator options will always take precedence over default options and default options will always take precedence over global options. This allows you to declare global rules while having the ability to override them in lower levels.

This rule does not apply to dependentKeys, instead they all are merged. In the example above, firstName's dependentKeys will be ['i18n.locale', 'disableValidations', 'foo', 'bar']

Options as Functions

All options can be functions which are processed lazily before validate is called. These functions are passed the model and attribute that is associated with the validator while also given that as their context, giving you access to all its properties.

Please note that the message option of a validator has its own signature.

const Validations = buildValidations({
  dob: validator('date', {
    description: 'Date of Birth',
    format(model, attribute) {
      return model.get('meta.date.format');
    },
    before(model, attribute) {
      return moment();
    },
    after(model, attribute) {
      return moment().subtract(120, 'years');
    }
  })
});

Nested Keys

When declaring object validations (not including Ember Data models), it is possible to validate child objects from the parent object.

import Ember from 'ember';
import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
  'acceptTerms': validator('inclusion', { in: [ true ] }),
  'user.firstName': validator('presence', true),
  'user.lasName': validator('presence', true),
  'user.account.number': validator('number')
});

export default Ember.Component.extend(Validations, {
  acceptTerms: false,
  user:  {
    firstName: 'John',
    lastName: 'Doe' ,
    account: {
      number: 123456,
    }
  },
  isFormValid: Ember.computed.alias('validations.isValid'),
});